import React,{Component} from 'react';
import {connect} from 'dva';
import {routerRedux} from 'dva/router';
import {createForm} from 'rc-form';
import { ToastTips } from '../../../../../../../../common/toastTips/tosat';

import {List,Modal,Picker } from 'antd-mobile';

import moment from 'moment';
import 'moment/locale/zh-cn';
// import enUs from 'antd-mobile/lib/date-picker/locale/en_US';

import pcd from './pcdData/pcdList';
import pcdConvert from './pcdData/pcdConvert';

const prompt = Modal.prompt;

class PcdSelect extends Component{
  constructor(props){
    super(props);
    this.state = {
      data: [],
      asyncValue: [],
    }
  }

  componentDidMount = () => {
    const {province, city, district} = this.props;
    let provinceNum = pcdConvert.provinceCNumber(province);
    let cityNum = pcdConvert.cityCNumber(city);
    let districtNum = pcdConvert.districtCNumber(city, district);
    let pcdValue = pcdConvert.pcdListConvert(provinceNum, cityNum, districtNum)
    this.setState({
      asyncValue:pcdValue ? pcdValue : [],
      data:pcd.pcdList()
    })
  }

  componentWillReceiveProps(newProps){
    // console.log('newProps', JSON.stringify(newProps));

    let provinceNum = pcdConvert.provinceCNumber(newProps.province);
    let cityNum = pcdConvert.cityCNumber(newProps.city);
    let districtNum = pcdConvert.districtCNumber(newProps.city,newProps.district);
    let pcdValue = pcdConvert.pcdListConvert(provinceNum,cityNum, districtNum)
    this.state.asyncValue = pcdValue ? pcdValue:[];

  }

  //确认pcd
  onOKPCDAction = (value) =>{
    // console.log(value);
    const d = [...this.state.data];
    const asyncValue = [...value];
    this.setState({
      data: d,
      asyncValue,
    });
    let provinceText = pcdConvert.provinceCText(value[0]);
    let cityText = pcdConvert.cityCText(value[1]);
    let districtText = pcdConvert.districtCText(value[2]);
    // console.log("province",provinceText);
    // console.log("city",cityText);
    // console.log("district",districtText);
    this.props.savePCD(provinceText,cityText,districtText)
  };

  render(){
    const {getFieldProps, getFieldError, getFieldDecorator, setFieldsValue} = this.props.form;

    return(
      <Picker
          data={this.state.data}
          cols="3"
          // cols={this.state.cols}
          extra="请选择"
          value={this.state.asyncValue}
          onPickerChange={this.onPickerChange}
          onOk = {this.onOKPCDAction}
        >
          <List.Item arrow="horizontal">地区</List.Item>
      </Picker>
    )
  }
}

const PcdSelectWrapper = createForm()(PcdSelect);
export default connect()(PcdSelectWrapper)
